package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.flight.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.domain.IflightOrder;
import com.ktgj.flight.service.IIflightOrderService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;
import sun.security.krb5.internal.Ticket;

/**
 * 国际机票订单Controller
 * 
 * @author ruoyi
 * @date 2022-05-31
 */
@RestController
@RequestMapping("/order")
public class IflightOrderController extends BaseController
{
    @Autowired
    private IIflightOrderService iflightOrderService;

    /**
     * 查询国际机票订单列表
     */
    @PreAuthorize(hasPermi = "system:order:list")
    @GetMapping("/list")
    public TableDataInfo list(IflightOrder iflightOrder)
    {
        startPage();
        List<IflightOrder> list = iflightOrderService.selectIflightOrderList(iflightOrder);
        return getDataTable(list);
    }

    /**
     * 导出国际机票订单列表
     */
    @PreAuthorize(hasPermi = "system:order:export")
    @Log(title = "国际机票订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IflightOrder iflightOrder) throws IOException
    {
        List<IflightOrder> list = iflightOrderService.selectIflightOrderList(iflightOrder);
        ExcelUtil<IflightOrder> util = new ExcelUtil<IflightOrder>(IflightOrder.class);
        util.exportExcel(response, list, "国际机票订单数据");
    }

    /**
     * 获取国际机票订单详细信息
     */
    @PreAuthorize(hasPermi = "system:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iflightOrderService.selectIflightOrderById(id));
    }

    /**
     * 新增国际机票订单
     */
    @PreAuthorize(hasPermi = "system:order:add")
    @Log(title = "国际机票订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IflightOrder iflightOrder)
    {
        return toAjax(iflightOrderService.insertIflightOrder(iflightOrder));
    }

    /**
     * 修改国际机票订单
     */
    @PreAuthorize(hasPermi = "system:order:edit")
    @Log(title = "国际机票订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IflightOrder iflightOrder)
    {
        return toAjax(iflightOrderService.updateIflightOrder(iflightOrder));
    }

    /**
     * 删除国际机票订单
     */
    @PreAuthorize(hasPermi = "system:order:remove")
    @Log(title = "国际机票订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iflightOrderService.deleteIflightOrderByIds(ids));
    }

    /**
     * pnr导入
     * */
    @PostMapping("/selectPNR")
    public AjaxResult selectPNR(@RequestBody Map<String, Object> map) {
        String pnr = map.get("pnr").toString();
        PNRVo pnrVo;
        try {
            pnrVo = iflightOrderService.selectPNR(pnr);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(pnrVo);
    }

    /**
     * 创建国际机票订单
     * */
    @PostMapping("/selectIflightOrder")
    public AjaxResult selectIflightOrder(HttpServletRequest request, @RequestBody IflightOrderVo iflightOrderVo) {
        int order;
        try {
            order = iflightOrderService.selectIflightOrder(request, iflightOrderVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (order > 0){
            return AjaxResult.success("创建成功");
        }
        else {
            return AjaxResult.error("创建失败");
        }
    }

    /**
     * 国际机票订单列表
     * */
    @PostMapping("/tmpIflightOrderVoList")
    public TableDataInfo tmpIflightOrderVoList(@RequestBody InternationalityVo internationalityVo) {
        startPage();
        List<IflightOrder> iflightOrders;
        try {
            iflightOrders = iflightOrderService.tmpIflightOrderVoList(internationalityVo);
        } catch (Exception e){
            return null;
        }
        return getDataTable(iflightOrders);
    }

    /**
     * 国际机票详情
     * */
    @PostMapping("/selectIflightPassengerById")
    public AjaxResult selectIflightPassengerById(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        IflightOrderVo iflightOrderVo;
        try {
            iflightOrderVo = iflightOrderService.selectIflightPassengerById(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(iflightOrderVo);
    }

    /**
     * 国际机票支付页
     * */
    @PostMapping("/selectTmpIflightOrderVo")
    public AjaxResult selectTmpIflightOrderVo(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        TmpIflightOrderVo tmpIflightOrderVo;
        try {
            tmpIflightOrderVo = iflightOrderService.selectTmpIflightOrderVo(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(tmpIflightOrderVo);
    }

    /**
     * 国际机票支付
     * */
    @PostMapping("/internationalTicketPay")
    public AjaxResult internationalTicketPay(HttpServletRequest request, @RequestBody InternationalPayVo internationalPayVo){
        PayResultVo payResultVo;
        try {
            payResultVo = iflightOrderService.internationalTicketPay(request, internationalPayVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(payResultVo);
    }

    /**
     * 国际机票退票详情
     * */
    @PostMapping("/selectRefundDetails")
    public AjaxResult selectRefundDetails(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        RefundVo refundVo;
        try {
            refundVo = iflightOrderService.selectRefundDetails(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(refundVo);
    }

    /**
     * 国际机票退款
     * */
    @PostMapping("/refund")
    public AjaxResult refund(HttpServletRequest request, @RequestBody CancelVo cancelVo){
        if (cancelVo != null){
            String msg;
            try {
                msg = iflightOrderService.refund(request, cancelVo);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            return AjaxResult.success(msg);
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 国际机票改签
     * */
    @PostMapping("/rebook")
    public AjaxResult rebook(HttpServletRequest request, @RequestBody IflightOrderVo iflightOrderVo){
        int i;
        try {
            i = iflightOrderService.rebook(request, iflightOrderVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("改签成功");
        }
        else {
            return AjaxResult.error("改签失败");
        }
    }
}
